"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("../config/index.js");
const utils = require("utils");
const sameDay = (d1, d2) => {
  return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate();
};
function formatter2Obj(content) {
  if (utils.util.nilValue(content)) {
    return null;
  }
  let jsonObj = null;
  if (typeof content === "string") {
    try {
      jsonObj = JSON.parse(content);
    } catch (error) {
    }
  } else if (typeof content === "object") {
    jsonObj = content;
  }
  return jsonObj;
}
function getTopRowData(rowArray) {
  let topRowIndex = rowArray.findIndex((e) => {
    let tmpArr = Array.isArray(e) ? e : [];
    let third = tmpArr[2];
    if (Array.isArray(third) && third.length >= 1) {
      let first = third[0];
      if (Array.isArray(first) && first.length >= 2) {
        let txt = first[1].value;
        txt = `${txt}`;
        return txt.indexOf("组别") >= 0;
      }
    }
    return false;
  });
  if (topRowIndex >= 0) {
    return topRowIndex;
  }
  return null;
}
function findTodayIndex(topRow, today) {
  let third = topRow[2];
  let thirdArr = Array.isArray(third) ? third : [];
  let matchIndex = thirdArr.findIndex((e) => {
    let tmpArr = Array.isArray(e) ? e : [];
    let valueObj = tmpArr[1] || {};
    let { value } = valueObj;
    if (typeof value === "number") {
      let baseDate = new Date(1900, 0, 0);
      baseDate.setDate(baseDate.getDate() + value - 1);
      return sameDay(today, baseDate);
    }
    return false;
  });
  if (matchIndex >= 0) {
    return matchIndex;
  }
  return null;
}
function runloopGetInput(dataArr) {
  let result = null;
  let currentSearch = dataArr;
  while (utils.util.nilValue(result)) {
    let isRight = false;
    if (Array.isArray(currentSearch)) {
      let third = currentSearch[2];
      if (Array.isArray(third)) {
        isRight = true;
        currentSearch = third;
      } else if (typeof third === "string") {
        result = third;
        isRight = true;
      }
    }
    if (!isRight) {
      return null;
    }
  }
  return result;
}
function getInput(valueObj) {
  let { value } = valueObj || {};
  if (typeof value === "string") {
    return value;
  }
  if (typeof value === "object") {
    if (Array.isArray(value.data)) {
      let input = runloopGetInput(value.data);
      if (input) {
        return input;
      }
    }
  }
  return "__不知道__";
}
function findTargetValue(dataArray, index2) {
  for (let i = 0; i < dataArray.length; i++) {
    let item = dataArray[i];
    if (Array.isArray(item) && item.length >= 2) {
      let curIndex = item[0];
      if (typeof curIndex === "number" && curIndex <= index2) {
        if (curIndex === index2) {
          return getInput(item[1]);
        }
      } else {
        return "__空数据__";
      }
    }
  }
}
function formatterGroupData(rowArray, topRowIndex, todayIndex) {
  let personDataList = rowArray.slice(topRowIndex + 1);
  let groups = [];
  let lastGroup = null;
  for (let i = 0; i < personDataList.length; i++) {
    let item = personDataList[i];
    let compareArr = null;
    if (Array.isArray(item) && item.length >= 3 && Array.isArray(item[2]) && item[2].length > 0) {
      compareArr = item[2];
    }
    if (!compareArr) {
      continue;
    }
    let first = compareArr[0];
    if (Array.isArray(first) && first.length >= 2) {
      let innerIndex = first[0];
      if (innerIndex === 0) {
        let groupName = getInput(first[1]);
        lastGroup = { des: groupName, list: [] };
        groups.push(lastGroup);
      }
    }
    if (lastGroup) {
      let name = findTargetValue(compareArr, 2);
      let input = findTargetValue(compareArr, todayIndex);
      lastGroup.list.push({
        name,
        input: input || ""
        // 可能今天没有填写...
      });
    }
  }
  return groups;
}
function formatterFormData(formData) {
  let { rows } = formData;
  let rowArray = Array.isArray(rows) ? rows : [];
  let topRowIndex = getTopRowData(rowArray);
  if (utils.util.nilValue(topRowIndex)) {
    return [new utils.BaseErr(-4008, "表格头没找到"), void 0];
  }
  let topRowData = rowArray[topRowIndex];
  let today = utils.util.UTC8ConverDate(/* @__PURE__ */ new Date());
  let todayIndex = findTodayIndex(topRowData, today);
  if (utils.util.nilValue(todayIndex)) {
    let todayDes = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`;
    return [new utils.BaseErr(-4008, `表格头里 没有今天 ${todayDes} 哦`), void 0];
  }
  return [void 0, formatterGroupData(rowArray, topRowIndex, todayIndex)];
}
function formatterContent(original) {
  let jsonObj = formatter2Obj(original);
  if (utils.util.nilValue(jsonObj)) {
    return [new utils.BaseErr(-4005, "加密 数据转成json 失败"), void 0];
  }
  let { content = {} } = jsonObj || {};
  let docData = content[index.docTable];
  if (utils.util.nilValue(docData)) {
    return [new utils.BaseErr(-4006, `没有目标 ${index.docTable} 表格的数据`), void 0];
  }
  return formatterFormData(docData);
}
exports.formatterContent = formatterContent;
